<?php
namespace app\admin\controller;

use think\Db;
use think\Request;
use app\admin\model\User as UserModel;

class User extends BaseController
{

    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'user';

    /**
     * 用户管理视图
     * @return mixed
     */
    public function index() {
        // 查询所有角色
        $result = DB::table("role")->order('sort', 'asc')->select();
        $this->assign("roles", $result);
        return $this->fetch();
    }

    /**
     * 分页查询
     * @param int $pageNumber
     * @param int $pageSize
     * @param string $searchText
     * @return \think\response\Json
     */
    public function getList($pageNumber = 1, $pageSize = 5, $searchText = '%', $type) {
        $user = new UserModel();
        // 分页查询
        $total = null;
            if ($type != null && $type != '') {
                $total = $user->whereLike('nickname', '%'.$searchText.'%')->where('type', $type)->paginate($pageSize, false, ["page"=>$pageNumber]);
            } else {
                $total = $user->whereLike('nickname', '%'.$searchText.'%')->paginate($pageSize, false, ["page"=>$pageNumber]);
            }
        return json($total);
    }

    /**
     * 添加用户
     * @param Request $request
     * @return \think\response\Json
     */
    public function addUser(Request $request) {
        // 获取参数仅包含数组中字段
        $data = $request->only(['username', 'nickname', 'password', 'email', 'status']);
        $data['password'] = $data['password'].'OES';
        // 添加数据
        $result = UserModel::create($data);
        // $result = DB::table($this->table)->insert($user);
        // 返回数据
        if ($result) {
            return json(['success'=>true, "msg"=>'添加成功']);
        } else {
            return json(['success'=>false, "msg"=>'添加失败']);
        }
    }

    /**
     * 修改用户
     * @param Request $request
     * @return \think\response\Json
     */
    public function updateUser(Request $request) {
        // 获取参数仅包含数组中字段
        $data = $request->only(['id', 'username', 'nickname', 'email', 'status']);
        // 更新用户
        //$result = DB::table($this->table)->update($user);
        $result = UserModel::update($data);
        // 返回数据
        if ($result) {
            return json(['success'=>true, "msg"=>'修改成功']);
        } else {
            return json(['success'=>false, "msg"=>'修改失败']);
        }
    }

    /**
     * 删除用户
     * @return \think\response\Json
     */
    public function delete() {
        $id = input('id');
        // 删除用户
        //$result = DB::table($this->table)->where("id", $id)->delete();
        $result = UserModel::destroy($id);
        // 返回数据
        if ($result) {
            return json(['success'=>true, "msg"=>'删除成功']);
        } else {
            return json(['success'=>false, "msg"=>'删除用户不存在']);
        }
    }

    /**
     * 测试查询数据
     * @param int $id
     * @return \think\response\Json
     */
    public function getId($id = 1) {
        $u1 = DB::table("user")->find($id);
        $u2 = UserModel::get($id);
        $u3 = UserModel::getByNickname('MM111');
        $u4 = UserModel::get(['nickname'=>'MM111']);
        $u5 = UserModel::where('nickname', 'MM')->find();
        $u6 = UserModel::all(['nickname'=>'test']);
        return json([$u1, $u2, $u3, $u4, $u5, $u6]);
    }

    public function testAdd() {
        $user = new UserModel();
        $user->nickname = 'XXXX';
        $r = $user->save();
        return json($r);
    }

}